<?php
include("./data/yingying/function.php");
class YingApi
{

	private $ConstantInfoPath = "./data/Original_god.json"; //常驻池信息路径
	private $guarantee = 90;
	private $count;
	public $Data = array("code" => 200);

	/**
	 * 接口运行
	 */
	public function Run($max)
	{
		if (!is_numeric($max)) {
			return array("code" => -2, "error" => "参数错误");
		}
		if ($max >= 2) {
			for ($i = 0; $i != $max; $i++) {
				$this->Get();
			}
		} else {
			$this->Get();
		}
		return $this->Data;
	}

	/**
	 * 抽卡实施
	 */
	private function Get()
	{
		$math = $this->MathRandom();
		$level = $this->GetCardValue($math);
		$ConstantInfo = $this->GetInfo($this->ConstantInfoPath);
		switch ($level) {
			case 3:
				$this->count++;
				$this->guarantee--;
				$pond = $ConstantInfo["r3"]["0"];
				$length = (count($pond) - 1);
				$round = round($this->MathRandom() * $length);
				$keyid = $pond[$round];
				$this->Data["total"]["r3"]++;
				$this->Data["list"][] = array("huo" => $keyid, "random" => $math, "type" => "r3");
				break;
			case 4:
				$this->count++;
				$this->guarantee--;
				$roundi = round($this->MathRandom());
				$pond = $ConstantInfo["r4"][$roundi];
				$length = (count($pond) - 1);
				$round = round($this->MathRandom() * $length);
				$keyid = $pond[$round];
				$this->Data["total"]["r4"]++;
				$this->Data["list"][] = array("huo" => $keyid, "random" => $math, "type" => "r4");
				break;
			default:
			case 5:
				$this->count++;
				$this->guarantee = 90;
				$roundi = round($this->MathRandom());
				$pond = $ConstantInfo["r5"][$roundi];
				$length = (count($pond) - 1);
				$round = round($this->MathRandom() * $length);
				$keyid = $pond[$round];
				$this->Data["total"]["r5"]++;
				$this->Data["list"][] = array("huo" => $keyid, "random" => $math, "type" => "r5");
				break;
		}
	}

	/**
	 * 浮点随机数
	 */
	private function MathRandom()
	{
		return 0 + mt_rand() / mt_getrandmax() * (1 - 0);
	}

	/**
	 * 获取卡值
	 */
	private function GetCardValue($math)
	{
		$level = 3;
		if ($math <= 0.006) {
			$level = 5;
		} else if ($math > 0.006 && $math <= 0.051) {
			$level = 4;
		} else {
			$level = 3;
		}
		if ($this->count > 3 && $this->guarantee == 1) {
			$level = 5;
		} else if ($this->count > 0 && (($this->count + 1) % 10 == 0)) {
			$level = 4;
		}
		return $level;
	}

	/**
	 * 获取卡牌总数据
	 */
	private function GetInfo($type)
	{
		$file = file_get_contents($type);
		return json_decode($file, true);
	}
}

$Api = new YingApi();
$max = $_REQUEST["num"];//次数
if (empty($max)) {
	Back(array("code" => -1, "error" => "参数不完整！"));
	die();
}
if ($max > 1000) {
	Back(array("code" => -3, "error" => "次数过大！"));
	die();
}
Back($Api->Run($max));
die();
